'use strict';

const { DataTypes } = require('sequelize');
const sequelize = require('../../database/sequelize');

/**
 * 游记模型
 */
const TravelNote = sequelize.define(
  'TravelNote',
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '游记ID'
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '用户ID',
      field: 'user_id'
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: '游记标题'
    },
    cover_image: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '封面图片URL',
      field: 'cover_image'
    },
    city: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '旅行城市'
    },
    start_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: '旅行开始日期',
      field: 'start_date'
    },
    end_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: '旅行结束日期',
      field: 'end_date'
    },
    content: {
      type: DataTypes.TEXT('long'),
      allowNull: false,
      comment: '游记内容'
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '游记简介（自动生成或手动填写）'
    },
    gallery_images: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '相册图片URLs（JSON数组）',
      field: 'gallery_images',
      get() {
        const raw = this.getDataValue('gallery_images');
        if (!raw) return [];
        try {
          return JSON.parse(raw);
        } catch (e) {
          return [];
        }
      },
      set(val) {
        this.setDataValue('gallery_images', JSON.stringify(val || []));
      }
    },
    tags: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '标签（JSON数组）',
      get() {
        const raw = this.getDataValue('tags');
        if (!raw) return [];
        try {
          return JSON.parse(raw);
        } catch (e) {
          return [];
        }
      },
      set(val) {
        this.setDataValue('tags', JSON.stringify(val || []));
      }
    },
    visibility: {
      type: DataTypes.ENUM('public', 'private'),
      allowNull: false,
      defaultValue: 'public',
      comment: '可见性：public-公开，private-私密'
    },
    status: {
      type: DataTypes.ENUM('draft', 'pending', 'approved', 'rejected'),
      allowNull: false,
      defaultValue: 'draft',
      comment: '状态：draft-草稿，pending-审核中，approved-已通过，rejected-已拒绝'
    },
    view_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '浏览量',
      field: 'view_count'
    },
    like_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '点赞数',
      field: 'like_count'
    },
    comment_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '评论数',
      field: 'comment_count'
    },
    favorite_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '收藏数',
      field: 'favorite_count'
    },
    is_featured: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否精选',
      field: 'is_featured'
    },
    is_top: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否置顶',
      field: 'is_top'
    },
    published_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '发布时间',
      field: 'published_at'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间',
      field: 'created_at'
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间',
      field: 'updated_at'
    }
  },
  {
    tableName: 'travel_notes',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        name: 'idx_user_id',
        fields: ['user_id']
      },
      {
        name: 'idx_city',
        fields: ['city']
      },
      {
        name: 'idx_status_visibility',
        fields: ['status', 'visibility']
      },
      {
        name: 'idx_created_at',
        fields: ['created_at']
      }
    ]
  }
);

module.exports = TravelNote;

